package tr.edu.ege.seagent.hobnobandroidapplication.activity;

import java.util.ArrayList;
import java.util.List;

import tr.edu.ege.seagent.hobnobandroidapplication.R;
import tr.edu.ege.seagent.hobnobandroidapplication.task.RetrieveLocationsOrCoordinatesTask;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class LocationListActivity extends Activity {

	public static final String LOCATION_SERVLET_URL = "http://155.223.24.19:8080/HobNobWebApp/AndroidLocation?resourceURI=";

	public final static String COORDINATE_SERVLET_URL = "http://155.223.24.19:8080/HobNobWebApp/Coordinate?resourceURI=";

	private static final String TAG = "LocationListActivity";

	private String clickedNotification;

	private ListView locationListView;

	/**
	 * Index of the selected location.
	 */
	private int clickedLocationPosition;

	public static final String SELECTED_NOTIFICATION_PARAM_NAME = "notification";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_location_list);
		// get notification resource URI.
		clickedNotification = getIntent().getStringExtra("notification");
		if (clickedNotification != null) {
			// getting location list from other thread...
			executeGetLocationListTask();
		}
	}

	/**
	 * This method is called by the async task after retrieving locations.
	 * 
	 * @param locationList
	 *            list of location to create view.
	 */
	private void handleRetrieveLocationsTaskResut(List<String> locationList) {
		// display location list on the screen with list view...
		displayLocationList(locationList);
		setClickListenerForListView();
	}

	/**
	 * This function provides us to display response's content as a ListView.
	 * 
	 * @param locationList
	 *            list of location resource URIs.
	 */
	private void displayLocationList(List<String> locationList) {
		Log.i(TAG, "displayLocationList METHOD");
		// Find the ListView resource.
		Log.d(TAG, "ListView is being gotten with ID.");
		locationListView = (ListView) findViewById(R.id.locationListView);
		Log.d(TAG, "ArrayAdapter is being created with given locationList.");
		ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this,
				R.layout.simple_row, locationList);
		// Set the ArrayAdapter as the ListView's adapter.
		Log.d(TAG, "ArrayAdapter is being set as the ListView's adapter");
		locationListView.setAdapter(listAdapter);
	}

	/**
	 * This function provide us to create new Activity class when clicked list
	 * view item.
	 */
	public void setClickListenerForListView() {
		locationListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				clickedLocationPosition = position;
				// get coordinates from http servlet...
				executeGetCoordinatesListTask();
			}

		});
	}

	/**
	 * Opens the map application for the requested location coordinates.
	 * 
	 * @param result
	 */
	private void handleRetrieveCoordinatesResult(List<String> result) {
		Log.i(TAG, "processCoordinates");
		// get x and y coordinate from the list...
		String[] locationCoordinate = result.get(clickedLocationPosition)
				.split(" ");
		String geoRequestString = "geo:0,0?q=" + locationCoordinate[0] + ","
				+ locationCoordinate[1] + "(Location)";
		Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(geoRequestString));
		startActivity(i);
	}

	/**
	 * Creates a {@link RetrieveLocationsOrCoordinatesTask} instance and
	 * executes it with {@link #COORDINATE_SERVLET_URL}.
	 */
	private void executeGetCoordinatesListTask() {
		new RetrieveLocationsOrCoordinatesTask(this).execute(
				clickedNotification, COORDINATE_SERVLET_URL);
	}

	/**
	 * This function provide us to get location list from AsyncTask.
	 */
	private void executeGetLocationListTask() {
		new RetrieveLocationsOrCoordinatesTask(this).execute(
				clickedNotification, LOCATION_SERVLET_URL);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_location_list, menu);
		return true;
	}

	/**
	 * This method is notified by the async task to change the view after
	 * executing the task.
	 * 
	 * @param locations
	 *            list of location URIs.
	 */
	public void processLocations(List<String> locations) {
		Log.i(TAG, "processLocations");
		handleRetrieveLocationsTaskResut(locations);
	}

	/**
	 * This method is notified by the async task to change the view after
	 * executing the task.
	 * 
	 * @param result
	 *            list of X and Y position strings.
	 */
	public void processCoordinates(ArrayList<String> result) {
		handleRetrieveCoordinatesResult(result);
	}
}
